package ar.edu.unsam.algo3.plantasvszombies.wicket

import ar.edu.unsam.algo3.plantasvszombies.ui.ApplicationModels.AlmanaqueAppModel
import ar.edu.unsam.algo3.plantasvszombies.wicket.components.XOnChangeAjaxBehavior
import org.apache.wicket.markup.html.basic.Label
import org.apache.wicket.markup.html.form.Form
import org.apache.wicket.markup.html.form.TextField
import org.uqbar.wicket.xtend.WicketExtensionFactoryMethods
import org.uqbar.wicket.xtend.XAjaxLink
import org.uqbar.wicket.xtend.XListView
import ar.edu.unsam.algo3.plantasvszombies.ui.ApplicationModels.JardinAppModel

class AlmanaqueZombiePage extends MainPage {
	extension WicketExtensionFactoryMethods = new WicketExtensionFactoryMethods
	val AlmanaqueAppModel almanaqueApp = new AlmanaqueAppModel
	val formZombies = new Form("formZombies")

	new(JardinAppModel unJardinApp) {
		jardinApp = unJardinApp
		defaultModel = almanaqueApp.asCompoundModel
		addChild(menuNavegacion)
		crearTablaZombies
		agregarAcciones

		almanaqueApp.buscar
	}

	def crearTablaZombies() {
		val listaZombie = new XListView("result")
		listaZombie.populateItem = [ item |
			item.model = item.modelObject.asCompoundModel
			item.addChild(new Label("nombre"))
			item.addChild(new Label("defensa"))
			item.addChild(new Label("ataque"))
			item.addChild(new Label("descripcion"))
		]

		formZombies.addChild(listaZombie)
		addChild(formZombies)
		formZombies.outputMarkupId = true
	}

	def agregarAcciones() {
		val txtNombre = new TextField<String>("nombre")
		txtNombre.addBehavior(
			new XOnChangeAjaxBehavior => [
				onUpdate = [ target |
					almanaqueApp.buscar
					target.add(formZombies)
				]])
		addChild(txtNombre)

		addChild(
			new XAjaxLink("limpiar") => [
				onClick = [ target |
					almanaqueApp.limpiar
					target.add(txtNombre)
					target.add(formZombies)
				]
			])
	}
}
